<?php
require_once '../sub/init.php';
header("Cache-control: private");
require_once 'chkUadm.php';
if(!in_array('u_add',$QXARR))exit(noauth());
if($submitok=="import"){
	require_once ZEAI.'sub/excel_reader.php';
	$file = $_FILES['execl'];
	$file_name = $file['tmp_name'];
	$data = new Spreadsheet_Excel_Reader();
	$data->setOutputEncoding('UTF-8');
	$data->read(iconv("UTF-8","GB2312",$file_name));
	//$data->sheets[0]['numRows']为Excel行数
	//$data->sheets[0]['numCols']为Excel列数
	$echo = '';
	$total=$data->sheets[0]['numRows'];
	$n=0;$s=0;
	for($i=2;$i<=$total;$i++){
		$mob      = trimhtml(dataIO($data->sheets[0]['cells'][$i][1],'in',11));
		$nickname = trimhtml(dataIO($data->sheets[0]['cells'][$i][2],'in',50));
		$sex      = trimhtml(dataIO($data->sheets[0]['cells'][$i][3],'in',2));
		$birthday = trimhtml(dataIO($data->sheets[0]['cells'][$i][4],'in',20));
		$heigh = trimhtml(dataIO($data->sheets[0]['cells'][$i][5],'in',20));
		$weigh = trimhtml(dataIO($data->sheets[0]['cells'][$i][6],'in',20));
		$love  = trimhtml(dataIO($data->sheets[0]['cells'][$i][7],'in',20));
		$edu   = trimhtml(dataIO($data->sheets[0]['cells'][$i][8],'in',100));
		$areatitle = trimhtml(dataIO($data->sheets[0]['cells'][$i][9],'in',100));
		$weixin = trimhtml(dataIO($data->sheets[0]['cells'][$i][10],'in',20));
		$identitynum = trimhtml(dataIO($data->sheets[0]['cells'][$i][11],'in',18));
		$truename = trimhtml(dataIO($data->sheets[0]['cells'][$i][12],'in',12));
		$aboutus = trimhtml(dataIO($data->sheets[0]['cells'][$i][13],'in',2000));
		$bz = trimhtml(dataIO($data->sheets[0]['cells'][$i][14],'in',500));
		$qq = trimhtml(dataIO($data->sheets[0]['cells'][$i][15],'in',100));
		$email = trimhtml(dataIO($data->sheets[0]['cells'][$i][16],'in',100));
		//数据对应转换
		if(!empty($mob)){
			if(ifmob($mob)){
				$row=$db->ROW(__TBL_USER__,"id","mob='$mob'");
				if($row){$s++;continue;}
			}else{
				$s++;continue;
			}
		}else{
			continue;
		}
		$nickname=dataIO($nickname,'in',50);
		if($sex=="女"){$sex=2;}else{$sex=1;}
		if(!ifdate($birthday)){$birthday='0000-00-00';}
		if(!empty($heigh)){$heigh=str_replace("cm","",$heigh);$heigh=intval($heigh);}else{$heigh=0;}
		if(!empty($weigh)){$weigh=str_replace("kg","",$weigh);$weigh=intval($weigh);}else{$weigh=0;}
		switch($love){
			case"未婚":$love=1;break;
			case"已婚":$love=2;break;
			case"离异":$love=3;break;
			case"丧偶":$love=4;break;
			case"保密":$love=5;break;
			default:$love=0;break;
		}
		switch($edu){case"初中":$edu=1;break;case"高中":$edu=2;break;case"大专":$edu=3;break;case"本科":$edu=4;break;case"硕士":$edu=5;break;case"博士":$edu=6;break;default:$edu=0;break;}
		//地区处理
		if(!empty($areatitle)){
			$A=explode('/',$areatitle);
			$AT1 = $A[0];
			$AT2 = $A[1];
			$AT3 = $A[2];
			$AT4 = $A[3];
			if (!empty($AT1)){
				$rowa = $db->ROW(__TBL_AREA1__,"id","title LIKE '%".$AT1."%'","num");
				if ($rowa){
					$a1 = intval($rowa[0]);	
					$areaid = $a1;
					if (!empty($AT2)){
						$rowa = $db->ROW(__TBL_AREA2__,"id","fid=".$a1." AND title LIKE '%".$AT2."%'","num");
						if ($rowa){
							$a2 = intval($rowa[0]);
							$areaid = $a1.','.$a2;
							if (!empty($AT3)){
								$rowa = $db->ROW(__TBL_AREA3__,"id","fid=".$a2." AND title LIKE '%".$AT3."%'","num");
								if ($rowa){
									$a3 = $rowa[0];
									$areaid = $a1.','.$a2.','.$a3;
									if (!empty($AT4)){
										$rowa = $db->ROW(__TBL_AREA4__,"id","fid=".$a3." AND title LIKE '%".$AT4."%'","num");
										if ($rowa){
											$a4 = $rowa[0];
											$areaid = $a1.','.$a2.','.$a3.','.$a4;
										}
									}
								}
							}
						}
					}
				}
			}
		}
		$areatitle= str_replace("/"," ",$areatitle);
		$RZ='mob';
		$regtime = ADDTIME;
		$endtime = ADDTIME;
		$refresh_time= ADDTIME;
		$regkind=12;
		$dataflag=1;
		$flag=1;
		$kind=intval($kind);
		$crm_fromclue=intval($crm_fromclue);
		$crm_ukind=intval($crm_ukind);

		//
		$db->query("INSERT INTO ".__TBL_USER__." (mob,sex,nickname,truename,birthday,heigh,weigh,love,areaid,areatitle,weixin,edu,RZ,aboutus,bz,crm_ubz,regtime,endtime,refresh_time,dataflag,flag,regkind,qq,email,kind,crm_ukind,crm_fromclue) VALUES ('$mob','$sex','$nickname','$truename','$birthday','$heigh','$weigh','$love','$areaid','$areatitle','$weixin','$edu','$RZ','$aboutus','$bz','$bz','$regtime','$endtime','$refresh_time','$dataflag','$flag','$regkind','$qq','$email','$kind','$crm_ukind','$crm_fromclue')");
		$uid = $db->insert_id();
		//
		$SQL = '';
		$uname = 'excel'.$uid;
		$pwd = md5($mob);
		if(empty($nickname))$nickname='uid'.$uid;
		//	
		if(ifint($agentid)){
			$row3 = $db->ROW(__TBL_CRM_AGENT__,"title","id=".$agentid,"num");
			if ($row3)$SQL .= ",agentid=$agentid,agenttitle='".$row3[0]."'";
		}else{$SETSQL .= ",agentid=0,agenttitle=''";}
		if($admid>0){
			$row3 = $db->ROW(__TBL_CRM_HN__,"truename","id=".$admid,"num");
			if ($row3)$SQL .= ",admid=$admid,admtime=".ADDTIME.",admname='".$row3[0]."'";
		}else{$SETSQL .= ",admid=0,admname=''";}
		//		
		$db->query("UPDATE ".__TBL_USER__." SET uname='$uname',pwd='$pwd'".$SQL." WHERE id=".$uid);
		//V6
		//set_data_ed_bfb($uid);
		//V7
		shalou_init($uid);
		//
		$row_shalou=shalou_data($uid);
		$row_shalou['truename']=$truename;
		$row_shalou['identitynum']=$identitynum;
		$new_row_shalou=encode_json($row_shalou);
		$db->query("UPDATE ".__TBL_SHALOU__." SET jsonstr='$new_row_shalou' WHERE uid=".$uid);
		//
		set_data_ed_bfb($uid);
		$n++;
	}
	if($n>0)AddLog('【Excel导入用户】->'.$n.'个');
	alert('成功导入：'.$n.'个，失败：'.$s.'个','back');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<style>
.box{min-width:1200px;padding:0}
.table {width:1000px;border:#F0F3F6 1px solid}
.table .tbodyT{font-size:18px;height:60px}
.table .tbodyT h6{color:#999;display:block}
</style>
<script src="<?php echo RES;?>www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/cache/udata.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="js/select2.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/res/select4.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script>
<?php
$rt=$db->query("SELECT id,title FROM ".__TBL_CRM_AGENT__." WHERE flag=1 ORDER BY px DESC,id DESC");
$total = $db->num_rows($rt);
if ($total > 0) {
	$arr=array();$arr2=array();
	for($i=0;$i<$total;$i++) {
		$rows = $db->fetch_array($rt,'name');
		if(!$rows) break;
		$arr2 = array();
		$rt2=$db->query("SELECT id,truename,username FROM ".__TBL_CRM_HN__." WHERE agentid=".$rows['id']." AND kind='crm' AND flag=1 ORDER BY px DESC,id DESC");
		$total2 = $db->num_rows($rt2);
		if ($total2 > 0) {
			for($i2=1;$i2<=$total2;$i2++) {
				$rows2 = $db->fetch_array($rt2,'name');
				if(!$rows2)break;
				$arr2[] = array('id'=>$rows2['id'],'name'=>$rows2['truename'].'　|　'.$rows2['username'].'　|　'.$rows2['id']);
			}
		}
		$arr[] = array('id'=>$rows['id'],'name'=>$rows['title'],'child'=>$arr2);
	}
	$agent_adm_data = encode_json($arr);
}
?>
var agent_adm_data = <?php echo $agent_adm_data;?>
</script>
</head>
<body>
<div class="box">
	<div class="boxL">
        <a href="u_add.php">用户人工录入</a>
        <a href="import.php" class="ed">Excel表导入</a>
        <a href="import_weiyuan.php">微缘数据导入</a>
        <a href="import_cfy.php">楚房云数据导入</a>
    </div>
	<div class="boxR">
        <form action="import.php?submitok=import" name="form_execl" method="post" enctype="multipart/form-data" onSubmit="zeai.msg('数据导入中，请匆关闭窗口...',{time:7200})">
        <table class="table" style="margin:15px 0 15px 20px">
            <tr>
                <td width="500" height="60" align="center"  class="tbodyT">
                <center>Excel表格数据导入</center>
                </td>
          </tr>
            <tr>
              <td class="border0 S14" style="padding:20px 0;background-color:#fff">
              <div style="width:590px;margin:0 auto;text-align:left">
              用户类型：<input type="radio" name="kind" value="1" id="kind_1" class="radioskin"><label for="kind_1" class="radioskin-label"><i class="i1"></i><b class="W120">线上用户</b></label>　　
            <input type="radio" name="kind" value="2" id="kind_2" class="radioskin" checked><label for="kind_2" class="radioskin-label"><i class="i1"></i><b class="W120">线下用户</b></label>　　
            <input type="radio" name="kind" value="3" id="kind_3" class="radioskin"><label for="kind_3" class="radioskin-label"><i class="i1"></i><b class="W120">均可(线上+线下)</b></label><br><br>
              线索来源：<script>zeai_cn__CreateFormItem('select','crm_fromclue','<?php echo $crm_fromclue; ?>','class="size2 W200"',crm_fromclue_ARR);</script>　<a href="crm_udata3.php" class="blue ">修改线索选项</a><br><br>
              客户分类：<script>zeai_cn__CreateFormItem('select','crm_ukind','<?php echo $crm_ukind; ?>','class="size2 W200"',crm_ukind_ARR);</script>　<a href="crm_udata3.php" class="blue">修改分类选项</a><br><br>
            <div>
            用户归属：<select name="agentid" id="agentid" class="size2 W200"></select> <select name="admid" id="admid" class="size2 W300"></select>
            <script>select2({id1:agentid,id2:admid,ext1:'class="size2 W200"',ext2:'class="size2 W300"',nulltext1:'请选择门店',nulltext2:'选择认领红娘',defv1:'<?php echo $session_agentid;?>',defv2:'<?php echo $session_uid;?>',data:agent_adm_data});</script>
			</div><br>
            上传表格：<input type="file" accept="application/vnd.ms-excel" name="execl" class="input size2 W200" /> <input type="submit" class="btn size3 " name="submit" value="开始导入" />
            </div>
				
            </td>
            </tr>
            <tr><td align="left" style="font-size:14px;line-height:200%;padding:20px;background-color:#fff">注意事项：<br>
            1、导入的Execl表格请勿私自改动数据结构，请用官方标准模版填入数据进行导入　<a href="mb.xls" class="btn size2 "><i class="ico">&#xe63c;</i>下载Excel模版</a><br>
            2、必须填写手机号，没有手机或库中已存在手机号将跳过此条信息，在导入过程中请勿关闭页面<br>
            3、如果遇到导入出错，请联系管理员<br>
            4、由于数据量太大，请确认PHP环境超时时间足够长，一般至少要10分钟，发送过程中请不要关闭窗口，耐心等待<br>
            5、有手机的新密码为手机号码，导入后推荐用UID+密码方式登录<br>
            6、如果提醒没有选中的文件，请用微软Excel打开数据另存为 Excel97-2003格式，如下图<br>
            <img src="images/excel_1.png"></td></tr>
        </table>
        </form>
    </div>
    <div class="clear"></div>
</div>
<?php require_once 'bottomadm.php';?>